define(function (require) {
    var Base = require('app/Base'),
        controller = require('../BaseController'),
        template = require('text!./iScrollPage.html'),
        model = {
            key1: 'iScroll'
        },
        myScroll;
    /**
     * 对外暴露函数，用于视图加载
     */
    var load = function () {
        render();
        bind();
        run();
    };
    // 判断浏览器是否支持localStorage
    if (window.Storage && window.localStorage && window.localStorage instanceof Storage) {
        var storage = window.localStorage;
    }

    function loaded() {
        myScroll = new IScroll('#wrapper', {
            scrollbars: true,
            mouseWheel: true,
            interactiveScrollbars: true,
            shrinkScrollbars: 'scale',
            fadeScrollbars: true
        });
        var li_index = storage.getItem("li_index");
        if (li_index) {
            li_index = parseInt(li_index);
            // 实现回滚
            myScroll.scrollToElement(document.querySelector('#scroller li:nth-child(' + li_index + ')'), null, null, true);
        }
    }

    /**
     * 视图渲染
     */
    function render() {
        controller.setTemplate(template);
        controller.setModel(model);
        controller.render(Base.getViewContainer());
    }

    /**
     * 事件绑定
     */
    function bind() {
    }

    /**
     * 除事件绑定
     */

    function run() {
        //获取所有li的节点
        var list = document.querySelectorAll('li a');
        //给每个li绑定事件
        for (var i = 0; i < list.length; i++) {
            list[i].id = 'li_' + (i + 1);//动态给每个a标签添加id
            list[i].onclick = function (i) {//动态给每个a标签添加onclick事件
                if (this.id) {
                    // 记录当前a的位置
                    storage.setItem("li_index", this.id.substring(3));
                }
            };
        }
        loaded();
    }

    return {
        load: load
    };
});